const mkapp = require(config.mkapp);
const db = require(config.db_url);
const express = require('express');
const router = express.Router();

let order = require(`${config.user_model}order.sql`);

const reg = require('./reg');
const login = require('./login');
const repass = require('./repass');
const index = require('./index');

const my = require('./my');
const safe = require('./safe');
const msg = require('./msg');


//必须登录才能进入会员中心
router.use((req, res, next)=>{
	
	if(!!!req.signedCookies.user && req.url != '/login' && req.url != '/reg' && req.url != '/repass'){
		res.redirect('/user/login');
	}else{
		next();
	}
});

//注册
router.use('/reg',reg);

//登陆
router.use('/login',login);

//找回密码
router.use('/repass',repass);

router.use(async(req, res, next)=>{
	try {
		if(!!!res.locals.score){
			let score = await db(res,order.score,[req.signedCookies.user]);
			let water = await db(res,order.queryWater,[req.signedCookies.user]);
			console.log(score);
			res.locals.score = score[0].score;
			res.locals.water = water[0].water;
			res.locals.face = water[0].face;
		}
		next();
	} catch (error) {
		console.error(error);
	}
});
//会员首页
router.use('/index',index);
//会员基本信息
router.use('/my',my);
//安全中心
router.use('/safe',safe);
//消息中心
router.use('/msg',msg);
module.exports = router;